<template>
  <div class="verify-email">
    <div class="email-container">
      <div class="status-icon">
        <!-- 根据状态展示不同的图标动画 -->
        <div v-if="loading" class="loading-spinner"></div>
        <img v-else-if="status === 'success'" src="@/assets/success-icon.png" alt="Success" class="status-image" />
        <img v-else-if="status === 'failure'" src="@/assets/failure-icon.png" alt="Failure" class="status-image" />
        <img v-else src="@/assets/error-icon.png" alt="Error" class="status-image" />
      </div>
      <h1 class="animated-title">邮箱绑定确认</h1>
      <p v-if="loading" class="animated-text">正在验证链接，请稍候...</p>
      <p v-else-if="status === 'success'" class="animated-text success-text">邮箱绑定成功！</p>
      <p v-else-if="status === 'failure'" class="animated-text failure-text">无效的链接或绑定已过期。</p>
      <p v-else class="animated-text error-text">未知错误，请稍后再试。</p>
      <button v-if="status === 'failure'" @click="retry" class="retry-button">重新验证</button>
    </div>
  </div>
</template>

<script>
import { emailVerify } from '@/api/user.js';

export default {
  data() {
    return {
      loading: true,
      status: null,
    };
  },
  mounted() {
    const urlParams = new URLSearchParams(window.location.search);
    const token = urlParams.get('token');
    if (token) {
      this.verifyEmailToken(token);
    } else {
      this.status = 'failure';
      this.loading = false;
    }
  },
  methods: {
    async verifyEmailToken(token) {
      try {
        const data = await emailVerify(token);
        this.status = !data.code ? 'success' : 'failure';
      } catch {
        this.status = 'failure';
      } finally {
        this.loading = false;
      }
    },
    retry() {
      this.loading = true;
      this.status = null;
      const urlParams = new URLSearchParams(window.location.search);
      const token = urlParams.get('token');
      if (token) {
        this.verifyEmailToken(token);
      } else {
        this.status = 'failure';
        this.loading = false;
      }
    },
  },
};
</script>

<style scoped>
.verify-email {
  text-align: center;
  background: linear-gradient(135deg, #722889, #9b59b6); /* 清华大学紫色渐变 */
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.email-container {
  padding: 30px;
  border-radius: 15px;
  background-color: #ffffff;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
  max-width: 400px;
  width: 90%;
  animation: fadeIn 1.2s ease-in-out;
}

.status-icon {
  margin-bottom: 20px;
}

.status-image {
  width: 80px;
  height: 80px;
  animation: bounce 1.5s infinite;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 4px solid #ddd;
  border-top-color: #722889; /* 紫色加载动画 */
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin: 0 auto;
}

.animated-title {
  font-size: 24px;
  font-weight: bold;
  color: #722889; /* 清华紫 */
  animation: slideInDown 0.8s ease-out;
}

.animated-text {
  font-size: 18px;
  margin-top: 10px;
  color: #555;
  animation: fadeIn 1.2s ease-in-out;
}

.success-text {
  color: #4caf50;
}

.failure-text {
  color: #f44336;
}

.error-text {
  color: #ff9800;
}

.retry-button {
  margin-top: 20px;
  padding: 10px 20px;
  font-size: 16px;
  color: #ffffff;
  background-color: #722889; /* 紫色按钮 */
  border: none;
  border-radius: 5px;
  cursor: pointer;
  animation: fadeIn 1.5s ease-in-out;
  transition: background-color 0.3s;
}

.retry-button:hover {
  background-color: #5e206d;
}

/* 动画效果 */
@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

@keyframes bounce {
  0%, 100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-15px);
  }
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes slideInDown {
  from {
    transform: translateY(-50px);
    opacity: 0;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}
</style>
